home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / clock2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-24  |  4.9 KB  |  226 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: clock2.c,v 4.2 88/06/22 14:37:27 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/clock2.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/clock2.c,v $$Revision: 4.2 $";
  12.  
  13. /* get today's date  (analog clock version) */
  14.  
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <stdio.h>
  18. #include "term.h"
  19. #include "restart.h"
  20.  
  21. #define OFFSET        (500<<10)        /* shift points to 1st quad */
  22.  
  23. /* sin, cos tables: 0-360 deg. in 6 deg. increments (scaled to +/- 1024) */
  24.  
  25. int sin[] = {
  26.      0, 107, 212, 316, 416, 512,
  27.      601, 685, 760, 828, 886, 935,
  28.      973, 1001, 1018, 1023, 1018, 1001,
  29.      973, 935, 886, 828, 760, 685,
  30.      601, 511, 416, 316, 212, 107,
  31.      0, -107, -212, -316, -416, -512,
  32.      -601, -685, -760, -828, -886, -935,
  33.      -973, -1001, -1018, -1023, -1018, -1001,
  34.      -973, -935, -886, -828, -760, -685,
  35.      -601, -511, -416, -316, -212, -107,
  36.      0 };
  37.  
  38.  
  39. int cos[] = {
  40.      1023, 1018, 1001, 973, 935, 886,
  41.      828, 760, 685, 601, 511, 416,
  42.      316, 212, 107, 0, -107, -212,
  43.      -316, -416, -512, -601, -685, -760,
  44.      -828, -886, -935, -973, -1001, -1018,
  45.      -1023, -1018, -1001, -973, -935, -886,
  46.      -828, -760, -685, -601, -511, -416,
  47.      -316, -212, -107, 0, 107, 212,
  48.      316, 416, 512, 601, 685, 760,
  49.      828, 886, 935, 973, 1001, 1018,
  50.      1023 };
  51.  
  52. typedef struct coord {
  53.     int x,y;
  54.     } coord;
  55.  
  56. /* coordinates of the hands at 12:00 */
  57.  
  58. coord second[] = { 1,-30,  0,485 };
  59. coord minute[] = { 10,-10,  0,400,  -10,-10,  10,-10 };
  60. coord hour[] = { 35,-10,  0,270,  -35,-10,  35,-10 };
  61.  
  62. coord big_tic[] = { -11,485,  0,450,  11,485 };
  63. coord tic[] = { 0,485,  0,460 };
  64.  
  65. int h, old_h, m, old_m, s, old_s;
  66.  
  67. main(argc,argv)
  68. int argc;
  69. char **argv;
  70.    {
  71.    register int i;
  72.    int cal=0;
  73.    int dotime(), clean();
  74.    char line[80];
  75.  
  76.    ckmgrterm( *argv );
  77.  
  78.    if (argc>1 && strcmp(argv[1],"-c")==0)
  79.       cal++;
  80.  
  81.    /* setup mgr environment */
  82.  
  83.    m_setup(0);
  84.    m_push(P_FLAGS|P_EVENT);
  85.    m_ttyset();
  86.  
  87.    m_setevent(REDRAW,"R\r");
  88.    m_setevent(RESHAPE,"R\r");
  89.    if (cal)
  90.       m_setevent(ACTIVATE,"A\r");
  91.  
  92.    signal(SIGALRM,dotime);
  93.    signal(SIGTERM,clean);
  94.    signal(SIGINT,clean);
  95.    signal(SIGHUP,clean);
  96.  
  97.    while(1) {
  98.       m_func(B_SET);
  99.       m_clear();
  100.       m_ellipse(500,500,490,490);
  101.       for(i=0;i<60;i+=5)         /* the tic marks */
  102.          if (i%15==0)
  103.             draw(big_tic,2,i);
  104.          else
  105.             draw(tic,1,i);
  106.    
  107.       m_movecursor(500,500);
  108.       get_time(&h, &m, &s);
  109.  
  110.       draw(hour,3,h);
  111.       draw(minute,3,m);
  112.  
  113.       m_func(B_INVERT);
  114.       draw(second,1,s);
  115.       m_flush();
  116.       dotime();
  117.  
  118.       /* wait for an event */
  119.  
  120.       while(1) {
  121.      extern int   errno;
  122.  
  123.          errno = 0;
  124.      *line = '\0';
  125.          if (m_gets(line) == NULL  &&  errno  &&  errno != EINTR)
  126.             clean(0);
  127.          alarm(0);
  128.          if (*line=='R')
  129.             break;
  130.          else if (cal && *line == 'A') {
  131.             long time(), tmp = time(0);
  132.             char *ctime();
  133.  
  134.             m_push(P_ALL);
  135.             m_font(11);
  136.             m_size(27,3);
  137.             m_clear();
  138.             m_printstr("\n "); 
  139.             m_printstr(ctime(&tmp));
  140.             m_flush();
  141.             sleep(3);
  142.             m_pop();
  143.             m_clearmode(M_ACTIVATE);
  144.             m_flush();
  145.             }
  146.          dotime();
  147.          }
  148.       }
  149.    }
  150.  
  151. /* update the time */
  152.  
  153. int
  154. dotime()
  155.    {
  156.    old_h=h, old_m=m, old_s=s;
  157.    get_time(&h, &m, &s);
  158.  
  159.    draw(second,1,old_s);
  160.    if (old_m != m) {
  161.       m_func(B_CLEAR);
  162.       draw(minute,3,old_m);
  163.       draw(hour,3,old_h);
  164.  
  165.       m_func(B_SET);
  166.       draw(hour,3,h);
  167.       draw(minute,3,m);
  168.       m_func(B_INVERT);
  169.       }
  170.  
  171.    draw(second,1,s);
  172.    m_flush();
  173.    signal(SIGALRM,dotime);
  174.    alarm(1);
  175.    }
  176.  
  177. /* rotate and draw hands */
  178.    
  179. #define X(i)    (what[i].x)
  180. #define Y(i)    (what[i].y)
  181.  
  182. draw(what, n, theta)
  183. coord what[];            /* array of points */
  184. int n;                /* number of points */
  185. int theta;            /* angle (in 160th of a circle) */
  186.    {
  187.    register int i;
  188.  
  189.    for(i=0;i<n;i++) {
  190.       m_line((X(i)*cos[theta]+Y(i)*sin[theta] + OFFSET)>>10,
  191.            (X(i)*sin[theta]-Y(i)*cos[theta] + OFFSET)>>10,
  192.            (X(i+1)*cos[theta]+Y(i+1)*sin[theta] + OFFSET)>>10,
  193.            (X(i+1)*sin[theta]-Y(i+1)*cos[theta] + OFFSET)>>10);
  194.            }
  195.    }
  196.  
  197. /* convert time to angles */
  198.  
  199. get_time(h,m,s)
  200. int *h, *m, *s;        /* hours, minutes, seconds */
  201.    {
  202.    struct tm *tme, *localtime();
  203.    long tmp,time();
  204.     
  205.    tmp = time(0);
  206.    tme = localtime(&tmp);
  207.  
  208.    *m = tme->tm_min;
  209.    *s = tme->tm_sec;
  210.    if (tme->tm_hour > 11)
  211.       tme->tm_hour -= 12;
  212.    *h = tme->tm_hour*5 + (2+tme->tm_min)/12;
  213.    }
  214.  
  215. /* clean up and exit */
  216.  
  217. clean(n)
  218. int    n;
  219.    {
  220.    m_popall();
  221.    m_clear();
  222.    m_flush();
  223.    m_ttyreset();
  224.    exit(1);
  225.    }
  226.